package com.qiche.controller.mgr;

import com.github.pagehelper.PageInfo;
import com.qiche.aspect.Result;
import com.qiche.constant.AccountType;
import com.qiche.model.Role;
import com.qiche.model.RoleResources;
import com.qiche.model.User;
import com.qiche.service.RoleResourcesService;
import com.qiche.service.RoleService;
import com.qiche.util.UserUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理控制器
 * Created by Linzw on 2018/01/24.
 */
@RestController
@RequestMapping("/roles")
public class RoleController {
	@Resource
	private RoleService roleService;
	@Resource
	private RoleResourcesService roleResourcesService;

	/**
	 * 根据条件 和分页获取角色列表
	 * @param role
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping
	public Map<String, Object> getAll(Role role,
			@RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int limit) {

		Map<String, Object> map = new HashMap<>();
		User currentUser=UserUtils.getCurrentUser();
		if(AccountType.ADMIN!=currentUser.getAccountType()){
			role.setMerchantId(currentUser.getMerchantId().longValue());
		}
		PageInfo<Role> pageInfo = roleService.selectByPage(role, page, limit);
		map.put("code", 0);
		map.put("msg", "查询成功");
		map.put("count", pageInfo.getTotal());
		map.put("data", pageInfo.getList());
		return map;
	}

	/**
	 * 根据用户ID获取 带勾选情况的 角色列表
	 * @param uid
	 * @return
	 */
	@RequestMapping("/rolesWithSelected")
	public Result rolesWithSelected(Integer uid) {
		List<Role> roles = null;
		User currentUser=UserUtils.getCurrentUser();
		if(AccountType.ADMIN==currentUser.getAccountType()){
			roles=roleService.queryAdminRoleListWithSelected(uid);
		}else{
			roles=roleService.queryRoleListWithSelected(uid);
		}
		return Result.createBySuccess(roles);
	}

	/**
	 * 保存角色和资源的对应关系
	 * @param roleResources
	 * @return
	 */
	@RequestMapping("/saveRoleResources")
	public Result saveRoleResources( RoleResources roleResources) {
		roleResourcesService.addRoleResources(roleResources);
		return Result.createBySuccessMessage("保存成功");
	}

	/**
	 * 添加角色信息
	 * @param role
	 * @return
	 */
	@PostMapping
	public Result add(@RequestBody Role role) {
		User currentUser=UserUtils.getCurrentUser();
		if(AccountType.ADMIN!=currentUser.getAccountType()){
			role.setMerchantId(currentUser.getMerchantId().longValue());
		}
		int isOk = roleService.save(role);
		if (isOk > 0) {
			return Result.createBySuccessMessage("保存成功");
		}
		return Result.createByErrorMessage("保存失败");

	}

	/**
	 * 根据ID删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete")
	public Result delete(Integer id) {
		return 	roleService.delRole(id);
	}


	/**
	 * 根据当前登录用户 的身份 获取 对用的角色列表
	 * @return
	 */
	@GetMapping(value = "/roles.json")
	public  Result  getRoles() {
		List<Role> roles=null;
		User currentUser=UserUtils.getCurrentUser();
		if(currentUser.getMerchantId()!=null){
			roles=roleService.selectRolesByMerchantId(currentUser.getMerchantId().longValue());
		}else{
			roles=roleService.selectRolesByMerchantId(null);
		}
		return Result.createBySuccess(roles);
	}


}
